var currentAuto;

function SpeedUp(mph) {
    if (currentAuto) {
        currentAuto.increaseSpeed(mph);
        WriteAutoDetails(currentAuto);
    }
}

function AutoOverHeated(sender, eventArgs) {
    var mask = "Max speed is {0}, and you tried to go {1} over the max. You are overheating";
    alert(String.format(mask, sender.get_maxSpeed(), eventArgs.OverMaxSpeed));
}

function WriteAutoDetails(auto) {
    if (currentAuto != auto) {
        currentAuto = auto;
        auto.add_overHeat(AutoOverHeated);
    }
    
    var sb = new Sys.StringBuilder('<div>');
    WriteLineToStringBuilder(sb, 'Object Name', Object.getType(auto).getName() );
    WriteLineToStringBuilder(sb, 'Max Speed', auto.get_maxSpeed() );
    WriteLineToStringBuilder(sb, 'Current Speed', auto.get_currentSpeed() );
    // proof there that there is no enforcement of the
    // encaspulation we built. It would be nice to not have access
    // to auto._maxSpeed, but as you can see; we do have access.
    WriteLineToStringBuilder(sb, 'Max Speed (no encapsulation)', auto._maxSpeed );
    WriteLineToStringBuilder(sb, 'Most Popular Color', auto.get_color() );
    WriteLineToStringBuilder(sb, 'Wheel Count', auto.get_wheelCount() );
    sb.append(' </div>');
    WriteResults( sb.toString() );
}

function WriteLineToStringBuilder(sb, label, value) {
    sb.append("<b>");
    sb.append( label );
    sb.append(" : </b>");
    sb.append( value );
    sb.append(" <br /> ");
}

function ClearSpeed() {
    WriteResults('');
}

function WriteResults(value) {
    var speed = $get('speedResults');
    speed.innerHTML = value;
}









